﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avanzis.QueryBuilder;
using Avanzis.QueryBuilder.Fluent;
using NUnit.Framework;

namespace Avanzis.Helpers.QueryBuilder.NUnit.Fluent
{
    [TestFixture]
    public class LikeTests
    {
        public class Article
        {
            public int ArticleId { get; set; }
            public string Name { get; set; }
            public bool Active { get; set; }
            public int UserId { get; set; }
            public int CategoryId { get; set; }
        }

        public class User
        {
            public int UserId { get; set; }
            public string FullName { get; set; }
            public int GroupId { get; set; }
        }

        public class Group
        {
            public int GroupId { get; set; }
            public string Name { get; set; }
        }

        [Test]
        public void EmptyLike_Generates_Expression()
        {
            var sb = new Select<Article>()
                .Field<Article>(a => a.ArticleId)
                .From<Article>()
                .Where<Article>(a => a.Name, SqlOperator.Like, string.Empty);

            var result = "SELECT Article.ArticleId\nFROM [Article]\nWHERE (Article.Name LIKE '')\n";

            Assert.AreEqual(result, sb.ToString());
            var cloned = (Select<Article>) sb.Clone();
            Assert.AreEqual(cloned.ToString(), sb.ToString());
        }
        
        [Test]
        public void SimpleLike_Generates_Expression()
        {
            var sb = new Select<Article>()
                .Field<Article>(a => a.ArticleId)
                .From<Article>()
                .Where<Article>(a => a.Name, SqlOperator.Like, "%title%");

            var result = "SELECT Article.ArticleId\nFROM [Article]\nWHERE (Article.Name LIKE '%title%')\n";

            Assert.AreEqual(result, sb.ToString());
            var cloned = (Select<Article>) sb.Clone();
            Assert.AreEqual(cloned.ToString(), sb.ToString());
        }
        
        [Test]
        public void ComplexLike_Generates_Expression()
        {
            var sb = new Select<Article>()
                .Field<Article>(a => a.ArticleId)
                .From<Article>();

            sb = sb.Where<Article>(a => a.Name, SqlOperator.Like, "%title word%");

            var result = "SELECT Article.ArticleId\nFROM [Article]\nWHERE (Article.Name LIKE '%title word%')\n";

            Assert.AreEqual(result, sb.ToString());
            var cloned = (Select<Article>) sb.Clone();
            Assert.AreEqual(cloned.ToString(), sb.ToString());
        }
        
        [Test]
        public void ComplexLike_With_Explicit_Type_Generates_Expression()
        {
            var sb = new Select<Article>()
                .Field<Article>(a => a.ArticleId)
                .From<Article>();

            sb = sb.Where<Article>(a => a.Name, SqlOperator.Like, "%title word%", ExpressionType.Primitive);

            var result = "SELECT Article.ArticleId\nFROM [Article]\nWHERE (Article.Name LIKE '%title word%')\n";

            Assert.AreEqual(result, sb.ToString());
            var cloned = (Select<Article>) sb.Clone();
            Assert.AreEqual(cloned.ToString(), sb.ToString());
        }
    }
}
